home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
Libraries
/
PlayerPRO 4.4.1
/
Filters Plugs
/
ToneGenerator.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-16
|
10KB
|
422 lines
/* ToneGenerator */
/* v 1.0 */
/* 1995 by ANR */
#include "MAD.h"
#include "PPPlug.h"
#include "math.h"
#if defined(powerc) || defined(__powerc)
enum {
PlayerPROPlug = kCStackBased
| RESULT_SIZE(SIZE_CODE( sizeof(OSErr)))
| STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( Ptr*)))
| STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( struct FileInstrData*)))
| STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( long)))
| STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( long)))
| STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( PPInfoPlug*)))
};
ProcInfoType __procinfo = PlayerPROPlug;
#else
#include <A4Stuff.h>
#endif
enum
{
silence = 3,
triangle = 4,
square = 5,
wave = 6
};
#define PI 3.1415926535897932384626433
#define KHZ 22254.54545
GDHandle TheGDevice:0xCC8;
void AutoPosition( DialogPtr aDia)
{
Point Position, mouse;
Rect ViewRect;
short XSize = (aDia->portRect.right - aDia->portRect.left), YSize = (aDia->portRect.bottom - aDia->portRect.top);
GetMouse( &mouse);
LocalToGlobal( &mouse);
SetRect( &ViewRect, (*TheGDevice)->gdRect.left + 8, (*TheGDevice)->gdRect.top + 43,
(*TheGDevice)->gdRect.right - 8, (*TheGDevice)->gdRect.bottom - 8);
Position.h = mouse.h - XSize/2;
if( Position.h + XSize >= ViewRect.right) Position.h = ViewRect.right - XSize;
else if( Position.h <= ViewRect.left) Position.h = ViewRect.left;
Position.v = mouse.v - YSize/2;
if( Position.v + YSize >= ViewRect.bottom) Position.v = ViewRect.bottom - YSize;
else if( Position.v <= ViewRect.top) Position.v = ViewRect.top;
MoveWindow( aDia, Position.h, Position.v, false);
ShowWindow( aDia);
}
void GetDText (DialogPtr dlog, short item, StringPtr str)
{
Handle itemHandle;
short itemType;
Rect itemRect;
GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
GetIText (itemHandle, str);
}
void SetDText (DialogPtr dlog, short item, Str255 str)
{
Handle itemHandle;
short itemType;
Rect itemRect;
GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
SetIText (itemHandle, str);
}
Ptr CreateAudio8Ptr( long AudioLength, long AudioFreq, long AudioAmp, long AudioType)
{
Ptr Audio8Ptr;
long i, temp, inter, x, dest;
Boolean UpDown;
Audio8Ptr = NewPtr( AudioLength);
if( Audio8Ptr == 0L) return 0L;
switch( AudioType)
{
case wave:
for( i = 0; i < AudioLength; i++)
{
temp = 127. * sin( ( ((double) i * (double) AudioFreq * PI * 2.) / KHZ));
/** Amplitude resizing **/
temp *= AudioAmp;
temp /= 100;
/** Overshoot **/
if( temp >= 127) temp = 127;
else if( temp <= -127 ) temp = -127;
Audio8Ptr[ i] = temp;
}
break;
case silence:
for( i = 0; i < AudioLength; i++) Audio8Ptr[ i] = 0x00;
break;
case square:
for( i = 0, x = 0, dest = -1; i < AudioLength; i++)
{
if( i > dest)
{
x++;
dest = (x * KHZ) / (AudioFreq * 2);
if( inter == -127) inter = 127;
else inter = -127;
}
temp = inter;
/** Amplitude resizing **/
temp *= AudioAmp;
temp /= 100;
/** Overshoot **/
if( temp >= 127) temp = 127;
else if( temp <= -127 ) temp = -127;
Audio8Ptr[ i] = temp;
}
break;
case triangle:
UpDown = true;
for( i = 0, x = 0, dest = -1; i < AudioLength; i++)
{
if( i > dest)
{
x++;
dest = (x * KHZ) / (AudioFreq * 2);
inter = dest - i;
UpDown = !UpDown;
}
if( UpDown) temp = (256 * (dest - i)) / inter;
else temp = (256 * (inter - (dest - i))) / inter;
temp -= 127;
/** Amplitude resizing **/
temp *= AudioAmp;
temp /= 100;
/** Overshoot **/
if( temp >= 127) temp = 127;
else if( temp <= -127 ) temp = -127;
Audio8Ptr[ i] = temp;
}
break;
}
return Audio8Ptr;
}
short* CreateAudio16Ptr( long AudioLength, long AudioFreq, long AudioAmp, long AudioType)
{
short *Audio16Ptr;
long i, temp, inter, x, dest;
Boolean UpDown;
Audio16Ptr = (short*) NewPtr( AudioLength*2);
if( Audio16Ptr == 0L) return 0L;
switch( AudioType)
{
case wave:
for( i = 0; i < AudioLength; i++)
{
temp = 32767. * sin( ( ((double) i * (double) AudioFreq * PI * 2.) / KHZ));
/** Amplitude resizing **/
temp *= AudioAmp;
temp /= 100;
/** Overshoot **/
if( temp >= (short) 0x7FFF) temp = 0x7FFF;
else if( temp <= (short) 0x8000 ) temp = (short) 0x8000;
Audio16Ptr[ i] = temp;
}
break;
case silence:
for( i = 0; i < AudioLength; i++) Audio16Ptr[ i] = 0x00;
break;
case square:
for( i = 0, x = 0, dest = -1; i < AudioLength; i++)
{
if( i > dest)
{
x++;
dest = (x * KHZ) / (AudioFreq * 2);
if( inter == -32767L) inter = 32767L;
else inter = -32767L;
}
temp = inter;
/** Amplitude resizing **/
temp *= AudioAmp;
temp /= 100;
/** Overshoot **/
if( temp >= (short) 0x7FFF) temp = 0x7FFF;
else if( temp <= (short) 0x8000 ) temp = (short) 0x8000;
Audio16Ptr[ i] = temp;
}
break;
case triangle:
UpDown = true;
for( i = 0, x = 0, dest = -1; i < AudioLength; i++)
{
if( i > dest)
{
x++;
dest = (x * KHZ) / (AudioFreq * 2);
inter = dest - i;
UpDown = !UpDown;
}
if( UpDown) temp = (65535L * (dest - i)) / inter;
else temp = (65535L * (inter - (dest - i))) / inter;
temp -= 32767L;
/** Amplitude resizing **/
temp *= AudioAmp;
temp /= 100;
/** Overshoot **/
if( temp >= (short) 0x7FFF) temp = 0x7FFF;
else if( temp <= (short) 0x8000 ) temp = (short) 0x8000;
Audio16Ptr[ i] = temp;
}
break;
}
return Audio16Ptr;
}
OSErr main( Ptr *InstrumentPtr,
struct FileInstrData *theData,
long SelectionStart,
long SelectionEnd,
PPInfoPlug *thePPInfoPlug)
{
long i, AudioLength, AudioFreq, AudioAmp;
Ptr Sample8Ptr = *InstrumentPtr, Audio8Ptr;
short *Sample16Ptr = (short*) *InstrumentPtr, *Audio16Ptr;
DialogPtr myDia;
short itemHit, itemType, AudioType;
Handle itemHandle;
Rect itemRect;
Str255 tStr;
OSErr iErr;
#ifndef powerc
long oldA4 = SetCurrentA4(); //this call is necessary for strings in 68k code resources
#endif
myDia = GetNewDialog( 128, 0L, (WindowPtr) -1L);
SetPort( myDia);
AutoPosition( myDia);
/** Default values **/
AudioType = silence;
GetDItem( myDia, AudioType, &itemType, &itemHandle, &itemRect);
SetCtlValue( (ControlHandle) itemHandle, 255);
AudioLength = SelectionEnd - SelectionStart;
if( theData->amplitude == 16) AudioLength /= 2;
if( AudioLength <= 0) AudioLength = 2000;
NumToString( AudioLength, tStr); SetDText( myDia, 8, tStr);
AudioFreq = 440; NumToString( AudioFreq, tStr); SetDText( myDia, 9, tStr);
AudioAmp = 100; NumToString( AudioAmp, tStr); SetDText( myDia, 10, tStr);
SelIText( myDia, 8, 0, 10000);
/********************/
Audio8Ptr = 0L; Audio16Ptr = 0L;
do
{
#if defined(powerc) || defined(__powerc)
ModalDialog( thePPInfoPlug->MyDlgFilterUPP, &itemHit);
#else
ModalDialog( (ModalFilterProcPtr) thePPInfoPlug->MyDlgFilterUPP, &itemHit);
#endif
switch( itemHit)
{
case 7:
GetDText( myDia, 8, tStr); StringToNum( tStr, &AudioLength);
GetDText( myDia, 9, tStr); StringToNum( tStr, &AudioFreq);
GetDText( myDia, 10, tStr); StringToNum( tStr, &AudioAmp);
switch( theData->amplitude)
{
case 8:
if( Audio8Ptr != 0L) { DisposPtr( Audio8Ptr); Audio8Ptr = 0L;}
Audio8Ptr = CreateAudio8Ptr( AudioLength, AudioFreq, AudioAmp, AudioType);
if( Audio8Ptr != 0L)
{
iErr = CallRPlaySoundUPP( Audio8Ptr, AudioLength, 0, 0, theData->amplitude, 0, 0);
}
break;
case 16:
if( Audio16Ptr != 0L) { DisposPtr( (Ptr) Audio16Ptr); Audio16Ptr = 0L;}
Audio16Ptr = CreateAudio16Ptr( AudioLength, AudioFreq, AudioAmp, AudioType);
if( Audio16Ptr != 0L)
{
iErr = CallRPlaySoundUPP( (Ptr) Audio16Ptr, AudioLength*2, 0, 0, theData->amplitude, 0, 0);
}
break;
}
break;
case 20:
case 19:
case 17:
case 18:
case 16:
case 3:
case 4:
case 5:
case 6:
switch( itemHit)
{
case 19:
case 20: itemHit = 3; break;
case 18: itemHit = 5; break;
case 17: itemHit = 4; break;
case 16: itemHit = 6; break;
}
for( i = 3; i <= 6;i ++)
{
GetDItem( myDia, i, &itemType, &itemHandle, &itemRect);
SetCtlValue( (ControlHandle) itemHandle, 0);
}
AudioType = itemHit;
GetDItem( myDia, AudioType, &itemType, &itemHandle, &itemRect);
SetCtlValue( (ControlHandle) itemHandle, 255);
break;
}
}while( itemHit != 1 && itemHit != 2);
if( itemHit == 1)
{
Ptr resultPtr;
GetDText( myDia, 8, tStr); StringToNum( tStr, &AudioLength);
GetDText( myDia, 9, tStr); StringToNum( tStr, &AudioFreq);
GetDText( myDia, 10, tStr); StringToNum( tStr, &AudioAmp);
if( Audio16Ptr != 0L) { DisposPtr( (Ptr) Audio16Ptr); Audio16Ptr = 0L;}
if( Audio8Ptr != 0L) { DisposPtr( (Ptr) Audio8Ptr); Audio8Ptr = 0L;}
switch( theData->amplitude)
{
case 8: Audio8Ptr = CreateAudio8Ptr( AudioLength, AudioFreq, AudioAmp, AudioType); break;
case 16:
Audio16Ptr = CreateAudio16Ptr( AudioLength, AudioFreq, AudioAmp, AudioType);
AudioLength *= 2;
break;
}
resultPtr = NewPtr( theData->insSize - (SelectionEnd - SelectionStart) + AudioLength);
BlockMove( *InstrumentPtr, resultPtr, SelectionStart);
if( theData->amplitude == 8) BlockMove( Audio8Ptr, resultPtr + SelectionStart, AudioLength);
else BlockMove( Audio16Ptr, resultPtr + SelectionStart, AudioLength);
BlockMove( *InstrumentPtr + SelectionEnd, resultPtr + SelectionStart + AudioLength, theData->insSize - SelectionEnd);
DisposPtr( *InstrumentPtr); DisposPtr( Audio8Ptr);
*InstrumentPtr = resultPtr;
theData->insSize = theData->insSize - (SelectionEnd - SelectionStart) + AudioLength;
}
DisposDialog( myDia);
#ifndef powerc
SetA4( oldA4);
#endif
return noErr;
}